layui.use(['form','treeSelect','layer','upload','laydate', 'layedit'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;
    var laydate = layui.laydate;
    var layedit = layui.layedit;

    //创建一个富文本编辑器
    layedit.set({
        height : 500,
        tool: [
            'strong' //加粗
            ,'italic' //斜体
            ,'underline' //下划线
            ,'del' //删除线
            ,'|' //分割线
            ,'left' //左对齐
            ,'center' //居中对齐
            ,'right' //右对齐
            ,'|' //分割线
            ,'link' //超链接
            ,'unlink' //清除链接
            ,'face' //表情
            ,'image' //插入图片
        ],
        uploadImage : {
            url : "/layedit/upload/pic"
        }
    });

    //图片上传 field: 'uploadFile',//上传文件的名称，要和接口参数名称相同
    upload.render({
        elem: '#articleCoverUpload',
        url: '/upload/pic',
        accept: 'images',
        acceptMime: 'image/*',
        field: "uploadFile", //设定文件域的字段名
        before: function(obj){
        },
        done: function(res){
            //上传完毕
            var url = res.url;
            $('#articleCover').val(url);
            $('.articleCoverImg').attr('src', res.url);
        }
    });

    //回显数据
    var request = GetRequest();
    $.get("/api/agt_article/" + request.id,function (data) {

        $("#id").val(data.id);
        $("#articleTitle").val(data.articleTitle);
        $("#articleAbstract").val(data.articleAbstract);
        // $("#articleContent").val(data.articleContent);
        $("#articleCover").val(data.articleCover);
        $("#createdBy").val(data.createdBy);
        $("#createdTime").val(data.createdTime);
        $("#updatedTime").val(data.updatedTime);

        //处理日期时间信息
        laydate.render({
            elem: '#createdTime'
            ,type: 'datetime'
            ,value: new Date(data.createdTime) //必须遵循format参数设定的格式
        });
        laydate.render({
            elem: '#updatedTime'
            ,type: 'datetime'
            ,value: new Date(data.updatedTime) //必须遵循format参数设定的格式
        });

        //所属物种
        treeSelect.render({
            elem: '#tree-select-resourceTypeId',
            data: '/api/agt_resource_type/tree/select/ef29c5bd67064812b94b921695a962ef',
            type: 'get',
            placeholder: '请选择物种分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function(data){
                $("#tree-select-resourceTypeId").val(data.current.id);
            },
            success: function (item) { // 加载完成后的回调函数 areaId
                var resourceTypeId = '0';
                if (data.resourceId != null && data.resourceId != "") {
                    $.ajax({
                        url: '/api/agt_resource/' + data.resourceId,
                        async: false,
                        success: function (d) {
                            resourceTypeId = d.resourceTypeId;
                        }
                    });
                    $("#tree-select-resourceTypeId").val(resourceTypeId);
                } else {
                    $("#tree-select-resourceTypeId").val("0");
                }
                treeSelect.checkNode('tree-select-resourceTypeId',resourceTypeId);
            }
        });

        //设置富文本编辑器内容
        window.editIndex = layedit.build('articleContent');
        layedit.setContent(editIndex, data.articleContent, false); //false是赋值模式，true是追加模式
        //用于同步编辑器内容到textarea
        layedit.sync(editIndex);

    });

    form.verify({
        content: function(value) {
            return layedit.sync(editIndex);
        }
    });
    //修改物种信息
    form.on('submit(edit)', function(data){
        /*data.field.createdTime = new Date(data.field.createdTime);
        data.field.updatedTime = new Date(data.field.updatedTime);
        console.log(data);*/
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/api/agt_article/edit',
            data:data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
});